function IceEvent() {
  this.events = {};
  this.keys=[];
}

IceEvent.prototype.subscribe = function(eventName, eventHandler) {
  if (this.events[eventName] === undefined) {
    this.events[eventName] = [];
  }
  this.events[eventName].push(eventHandler);
};

IceEvent.prototype.unsubscribe = function(eventName, eventHandler) {
  let existedEventHandlers = this.events[eventName];

  if (!existedEventHandlers) {
    //如果key对应的消息没有被人订阅，则直接返回
    return;
  }
  if (!eventHandler) {
    //如果没有传入具体的回调函数，标示取消key对应消息的所有订阅
    this.events[key] = [];
  } else {
    //倒序查找，从最新加入的开始判断
    for (let i = existedEventHandlers.length - 1; i >= 0; i--) {
      //取消key对应的订阅消息
      if (eventHandler.toString() === existedEventHandlers[i].toString()) {
        existedEventHandlers.splice(i, 1); //删掉原数组的此方法
        break;
      }
    }
  }
};

IceEvent.prototype.trigger = function(eventName, ...args) {
  //绑定发布事件
  let handlers = this.events[eventName];
  for (let i = 0; i < handlers.length; i++) {
    handlers[i](args);
  }
};

IceEvent.prototype.triggerOnce = function(eventName, ...args) {
  this.trigger(eventName, ...args)
  this.unsubscribe(eventName);
};
IceEvent.prototype.add = IceEvent.prototype.subscribe;
IceEvent.prototype.on = IceEvent.prototype.subscribe;

IceEvent.prototype.remove = IceEvent.prototype.unsubscribe;
IceEvent.prototype.off = IceEvent.prototype.unsubscribe;

IceEvent.prototype.emit = IceEvent.prototype.trigger;